package com.mentaleye.biz.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.mentaleye.dal.model.Prompt;
import com.mentaleye.enums.CommonStatus;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.PromptReadBean;
import com.mentaleye.biz.PromptBiz;
import com.mentaleye.service.PromptReadService;
import com.mentaleye.service.PromptService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
@Service
public class PromptBizImpl implements PromptBiz {

	@Autowired private PromptService promptService;
	@Autowired private PromptReadService promptReadService;
	private static Logger logger = Logger.getLogger(PromptBizImpl.class);

	@Override
	public boolean promptSave(String promptContext, Date endDate, String promptRoleId, String promptPersonId, CommonStatus commonStatus) {

		return promptService.promptSave(promptContext,endDate,promptRoleId,promptPersonId,commonStatus);
	}

	@Override
	public boolean promptSave(String promptContext, Date endDate, CommonStatus commonStatus) {
		return promptService.promptSave(promptContext,endDate,commonStatus);
	}

	@Override
	public boolean promptSave(int classId, Prompt prompt, CommonStatus commonStatus) {
		return promptService.promptSave(classId,prompt,commonStatus);
	}

	@Override
	public void getPromptByType(CommonPage commonPage) {
		//提醒类型
		/*Map<String, Object> map = commonPage.pageToMap();
		Integer.parseInt(map.get("promptType").toString());*/
		//TODO 根据登录用户id，取得所属的角色id。promptRoles
		//测试数据
		List<Integer> promptRoles = new ArrayList<Integer>();
		promptRoles.add(0, 1);
		promptRoles.add(1, 3);
		//TODO 获取登录用户的id promptPersonId
		//测试数据
		String promptPersonId = "461";
		//通过类型，人员编号查询提醒
		
		List<Map<String, Object>> data = promptService.queryPromptByTypeAndPerson(promptRoles, promptPersonId, commonPage.pageToMap());
		commonPage.setPageData(addParameter(data));

	}
	
	public List<Map<String, Object>> addParameter(List<Map<String, Object>> data){
		boolean isContainsKey;
		long days = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (Map<String, Object> map : data) {
			isContainsKey=map.containsKey("readTimes");//如果包含这个key，说明已查看
			if(isContainsKey){
				map.put("read", "已读");
			}else{
				map.put("read", "未读");
				map.put("isNotRead", 0);
			}
			try {
				Date now = sdf.parse((sdf.format(new Date())));
				Date end = sdf.parse(map.get("endDate").toString());
				days = (end.getTime()-now.getTime())/(1000*3600*24);
			} catch (ParseException e) {
				logger.error("求提醒时间差异常", e);
			}
			map.put("days", days);
		}
		return data;
	}
	
	@Override
	public List<Map<String, Object>> getCount() {
		List<Integer> promptRoles = new ArrayList<Integer>();
		promptRoles.add(0, 1);
		promptRoles.add(1, 3);
		String promptPersonId = "461";
		try {
			List<Map<String, Object>> listMap = promptService.queryPromptCount(promptRoles, promptPersonId);
			return listMap;
		} catch (Exception e) {
			logger.error("查询提醒条数异常", e);
			return null;
		}
		
	}
	@Override
	public CommonResult updateReadStatus(Integer id) {
		CommonResult commonResult = new CommonResult();
		String promptPersonId = "461";
		boolean isSuccess = false;
		try {
			PromptReadBean bean = promptReadService.queryPromptRead(id, promptPersonId);
			if(bean!=null){
				isSuccess = promptReadService.modifyPromptRead(id, promptPersonId, bean.getReadTimes()+1);
			}else{
				isSuccess = promptReadService.addPromptRead(id, promptPersonId);
			}
			commonResult.setIsSuccess(isSuccess);
			return null;
		} catch (Exception e) {
			logger.error("修改读取状态异常", e);
			return commonResult;
		}
		
	}

	@Override
	public CommonResult updateRead(List<Integer> ids, String status) {
		CommonResult commonResult = new CommonResult();
		String promptPersonId = "461";
		boolean isSuccess = false;
		if("read".equals(status)){
			isSuccess = promptReadService.updatePromptRead(ids, promptPersonId);//标记为已读
		}else{
			isSuccess = promptReadService.updatePromptNoRead(ids, promptPersonId);
		}
		commonResult.setIsSuccess(isSuccess);
		return commonResult;
	}

}
